﻿using System;
using System.Collections.Generic;
using System.Text;
using Kitware.VTK;

namespace Fu3DEngine
{
   public  class Image3DNode:NodeBase 
    {
       public Image3DNode(string id ) :base(id) 
       {
       }

       public override void Run()
       {
           base.Run();

           //this.Create3dImage();

           this.CreateVolumeUnsignedChar();


       }

       protected void Create3dImage()
       {
           //现在的函数只是用来测试基本的属性
           Random rd=new Random ();
           


           //float[,] pts=new float [64000,3];

           //for (int i = 0; i < 64000; i++)
           //{
           //    for (int j = 0; j < 3; j++)
           //    {
           //        pts[i][j] = (float)rd.NextDouble();

           //    }
               
           //}

           double[] scalars = new double[64000];


           for (int i = 0; i < 64000; i++)
           {
               scalars[i] = (float)rd.NextDouble();
               
           }




           vtkDoubleArray array = vtkDoubleArray.New();
           array.SetName("3ddata");
           array.SetNumberOfComponents(1);
           array.SetNumberOfTuples(64000);
           for (int i = 0; i < 64000; i++)
           {
               array.SetTuple1(i,scalars [i]);
           }




           


           vtkImageData img = vtkImageData.New();
           img.GetPointData().SetScalars(array);
           img.SetDimensions(40,40,40);
        
           img.SetScalarTypeToDouble();

           img.SetNumberOfScalarComponents(1);
           img.SetSpacing(1,1,1);
           img.SetOrigin(0, 0, 0);



           vtkImageDataGeometryFilter dgf = vtkImageDataGeometryFilter.New();
           dgf.SetInput(img);



           vtkOutlineFilter outline = vtkOutlineFilter.New();
           outline.SetInput(dgf.GetOutput ());


         

           



           //vtkImageGridSource gridS = vtkImageGridSource.New();
           //gridS.SetDataScalarTypeToDouble();
           //gridS.SetGridOrigin(412579, 137800, 200);
           //gridS.SetGridSpacing(3, 3, 1);
           //gridS.SetDataExtent(0, 128, 0, 128, 0, 128);
           //gridS.SetLineValue(4095);
           //gridS.SetFillValue(0);


           //vtkContourFilter cf = vtkContourFilter.New();
           //cf.SetInput(img);
           //cf.SetNumberOfContours(5);
         


           //vtkImageDataGeometryFilter dg = vtkImageDataGeometryFilter.New();
           //dg.SetInput(gridS .GetOutput ());

          


           vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
           mapper.SetInput(outline.GetOutput ());

           vtkActor actor=vtkActor .New ();
           actor.SetMapper (mapper);

           this.SParent .m_vtkRenderer .AddActor (actor);

           //切割三维用的
           vtkCellPicker picker = vtkCellPicker.New();
           picker.SetTolerance(0.005);

           vtkProperty ipwProperty = vtkProperty.New();


           vtkLookupTable lt = vtkLookupTable.New();
           


           vtkImagePlaneWidget p = vtkImagePlaneWidget.New();
           p.SetInteractor(SParent .m_vtkRenderWindow.GetInteractor ());

           //p.SetKeyPressActivationValue("x");

           //p.setkey

           p.SetPicker(picker);
           p.RestrictPlaneToVolumeOn();

           p.GetPlaneProperty().SetColor(1,0,0);

           p.SetTexturePlaneProperty(ipwProperty );

           p.TextureInterpolateOff();

           p.SetResliceInterpolateToNearestNeighbour();

           p.SetInput(img);


           p.SetPlaneOrientationToZAxes();

           p.SetSliceIndex(15);

           p.DisplayTextOn();

           p.On();

           p.InteractionOff();
           p.InteractionOn();


           p.SetLookupTable(lt);
        
           
          
           
           


           //vtkImageViewer viewer = vtkImageViewer.New();
           //viewer.SetInput(gra .GetOutput ());
           //viewer.SetColorWindow(2000);
           //viewer.SetColorLevel(1000);

           //viewer.SetZSlice(60);

           //viewer.Render();


 
       }



       //这个函数只是测试用的函数
       public void CreateVolume()
       {


           Random rd = new Random();

           double[] scalars = new double[64000];

           for (int i = 0; i < 64000; i++)
           {
               scalars[i] = (float)rd.NextDouble();

           }



           vtkDoubleArray array = vtkDoubleArray.New();
           array.SetName("3ddata");
           array.SetNumberOfComponents(1);
           array.SetNumberOfTuples(64000);
           for (int i = 0; i < 64000; i++)
           {
               array.SetTuple1(i, scalars[i]);
           }


           





           vtkImageData img = vtkImageData.New();
           img.GetPointData().SetScalars(array);
           img.SetDimensions(40, 40, 40);

           img.SetScalarTypeToDouble();

           img.SetNumberOfScalarComponents(1);
           img.SetSpacing(1, 1, 1);
           img.SetOrigin(0, 0, 0);


        



           vtkPiecewiseFunction piece = vtkPiecewiseFunction.New();
           piece.AddPoint(0,0.0);
           piece.AddPoint(1.0, 0.9);

           vtkColorTransferFunction colorTran = vtkColorTransferFunction.New();
           colorTran.AddRGBPoint(0.0,0.0,0.0,0.0);
           colorTran.AddRGBPoint(0.2,1.0,0.0,0.0);
           colorTran.AddRGBPoint(0.4, 0.0, 1.0, 0.0);

           colorTran.AddRGBPoint(0.6, 0.0, 0.0, 1.0);
           colorTran.AddRGBPoint(1.0, 0.0, 0.2, 0.0);


           vtkVolumeProperty volumeProperty = vtkVolumeProperty.New();
           volumeProperty.SetColor(colorTran );
           volumeProperty.SetScalarOpacity(piece );


           vtkVolumeRayCastCompositeFunction compositeFunc = vtkVolumeRayCastCompositeFunction.New();

           vtkVolumeRayCastMapper volumeMapper = vtkVolumeRayCastMapper.New();
           volumeMapper.SetInput(img);
           volumeMapper.SetVolumeRayCastFunction(compositeFunc );



           vtkVolume volume = vtkVolume.New();
           volume.SetMapper(volumeMapper );
           volume.SetProperty(volumeProperty );


           this.SParent.m_vtkRenderer.AddVolume(volume);



           


       }

       public void CreateVolumeUnsignedChar()
       {

           Random rd = new Random();

           //double[] scalars = new double[64000];


           //for (int i = 0; i < 64000; i++)
           //{
           //    scalars[i] = (float)rd.NextDouble();

           //}


           //vtkDoubleArray array = vtkDoubleArray.New();
           //array.SetName("3ddata");
           //array.SetNumberOfComponents(1);
           //array.SetNumberOfTuples(64000);
           //for (int i = 0; i < 64000; i++)
           //{
           //    array.SetTuple1(i, scalars[i]);
           //}

           ushort[] scalars=new ushort[64000];

           for (int i = 0; i < 64000; i++)
           {
               scalars[i] = (ushort)(rd.NextDouble()*255);

           }


           vtkUnsignedCharArray uarray = vtkUnsignedCharArray.New();
           uarray.SetName("3ddata");
           uarray.SetNumberOfComponents(1);
           uarray.SetNumberOfTuples(64000);
           for (int i = 0; i < 64000; i++)
           {
               uarray.SetTuple1(i, scalars[i]);
           }




           vtkImageData img = vtkImageData.New();
           img.GetPointData().SetScalars(uarray);
           img.SetDimensions(40, 40, 40);

           img.SetScalarTypeToDouble();

           img.SetNumberOfScalarComponents(1);
           img.SetSpacing(1, 1, 1);
           img.SetOrigin(0, 0, 0);



           //vtkImageCast cast = vtkImageCast.New();
           //cast.SetInput(img);
           //cast.SetOutputScalarTypeToUnsignedChar();



           vtkPiecewiseFunction piece = vtkPiecewiseFunction.New();
           piece.AddPoint(20, 0.0);
           piece.AddPoint(255, 0.2);

           vtkColorTransferFunction colorTran = vtkColorTransferFunction.New();
           colorTran.AddRGBPoint(0.0, 0.0, 0.0, 0.0);
           colorTran.AddRGBPoint(64, 1.0, 0.0, 0.0);
           colorTran.AddRGBPoint(128, 0.0, 1.0, 0.0);

           colorTran.AddRGBPoint(192, 0.0, 0.0, 1.0);
           colorTran.AddRGBPoint(255, 0.0, 0.2, 0.0);


           vtkVolumeProperty volumeProperty = vtkVolumeProperty.New();
           volumeProperty.SetColor(colorTran);
           volumeProperty.SetScalarOpacity(piece);
           volumeProperty.SetInterpolationTypeToLinear();
           


           vtkVolumeRayCastCompositeFunction compositeFunc = vtkVolumeRayCastCompositeFunction.New();
           

           vtkVolumeRayCastIsosurfaceFunction isoFunc = vtkVolumeRayCastIsosurfaceFunction.New();

           vtkVolumeRayCastMIPFunction mipFunc = vtkVolumeRayCastMIPFunction.New();


           //vtkFixedPointVolumeRayCastMapper volumeMapper = vtkFixedPointVolumeRayCastMapper.New();

           vtkVolumeRayCastMapper volumeMapper = vtkVolumeRayCastMapper.New();
           volumeMapper.SetInput(img);
           volumeMapper.SetVolumeRayCastFunction(compositeFunc);
           volumeMapper.SetSampleDistance(0.5);


           vtkVolume volume = vtkVolume.New();
           volume.SetMapper(volumeMapper);
           volume.SetProperty(volumeProperty);
           


           this.SParent.m_vtkRenderer.AddVolume(volume);






       }

       public override void ReLoad()
       {
           throw new NotImplementedException();
       }

       public override void Unload()
       {
           base.Unload();

       }




   }
   

  
}
